package com.hzncc.flowable_diboot.controller.report;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.D;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Status;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.file.controller.BaseExcelFileController;
import com.diboot.file.excel.listener.ReadExcelListener;
import com.diboot.file.util.ExcelHelper;

import com.hzncc.flowable_diboot.entity.report.WeeklySummaryReport;
import com.hzncc.flowable_diboot.dto.report.WeeklySummaryReportDTO;
import com.hzncc.flowable_diboot.service.report.WeeklySummaryReportService;
import com.hzncc.flowable_diboot.vo.report.WeeklySummaryReportListVO;
import com.hzncc.flowable_diboot.excel.listener.WeeklySummaryReportImportListener;
import com.hzncc.flowable_diboot.excel.model.WeeklySummaryReportImportModel;
import com.hzncc.flowable_diboot.excel.model.WeeklySummaryReportExportModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import lombok.extern.slf4j.Slf4j;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* 周报报表 Excel处理Controller
*
* @author MyName
* @version 1.0
* @date 2024-11-25
 * Copyright © MyCorp
*/
@Slf4j
@RequestMapping("/report/weekly-summary-report/excel")
@RestController
@BindPermission(name = "周报报表导入导出", code = "WeeklySummaryReport")
public class WeeklySummaryReportExcelController extends BaseExcelFileController {

    @Autowired
    private WeeklySummaryReportService weeklySummaryReportService;

    /**
    * 预览数据
    *
    * @param file
    * @param params
    * @throws Exception
    */
    @Log(operation = "导入预览", businessObj = "WeeklySummaryReport")
    @BindPermission(name = "导入预览", code = OperationCons.CODE_IMPORT)
    @PostMapping("/preview")
    public JsonResult preview(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreview(file, params);
    }

    /**
    * 导入预览保存
    *
    * @param params
    * @throws Exception
    */
    @Log(operation = "导入预览保存", businessObj = "WeeklySummaryReport")
    @BindPermission(name = "导入预览保存", code = OperationCons.CODE_IMPORT)
    @PostMapping("/preview-save")
    public JsonResult previewSave(@RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreviewSave(params);
    }

    /**
    * 上传excel并保存
    *
    * @param file
    * @param params
    * @throws Exception
    */
    @Log(operation = "直接导入", businessObj = "WeeklySummaryReport")
    @BindPermission(name = "直接导入", code = OperationCons.CODE_IMPORT)
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.uploadExcelFile(file, params);
    }

    /**
    * 下载示例文件
    *
    * @param response
    * @throws Exception
    */
    @Log(operation = "导入周报报表示例文件下载", businessObj = "WeeklySummaryReport")
    @BindPermission(name = "导入周报报表示例文件下载", code = OperationCons.CODE_IMPORT)
    @GetMapping("/download-example")
    public void downloadExample(HttpServletResponse response) throws Exception{
        String fileName = "周报报表导入示例.xlsx";
        ExcelHelper.exportExcel(response, fileName, WeeklySummaryReportImportModel.class, null);
    }

    /**
    * 周报报表列表导出
    *
    * @param response
    * @return
    * @throws Exception
    */
    @Log(operation = "导出列表文件", businessObj = "WeeklySummaryReport")
    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
    @GetMapping("/export")
    public JsonResult export(WeeklySummaryReportDTO weeklySummaryReportDto, @RequestParam(value = "columns", required = false) List<String> columns,
        HttpServletResponse response) throws Exception{
        QueryWrapper<WeeklySummaryReport> queryWrapper = super.buildQueryWrapperByQueryParams(weeklySummaryReportDto);
                List<WeeklySummaryReportListVO> weeklySummaryReportList = weeklySummaryReportService.getViewObjectList(queryWrapper, null, WeeklySummaryReportListVO.class);
        if (V.isEmpty(weeklySummaryReportList)) {
            return new JsonResult(Status.FAIL_OPERATION, "周报报表列表为空，导出失败");
        }
        String fileName = "周报报表列表导出_" + D.today() + ".xlsx";
        List<WeeklySummaryReportExportModel> dataList = this.entityList2ExcelList(weeklySummaryReportList);
        ExcelHelper.exportExcel(response, fileName, WeeklySummaryReportExportModel.class, columns, dataList);
        return null;
    }
    /**
    * 获取表格导出头
    *
    * @return
    */
    @GetMapping("/export-table-head")
    public JsonResult getTableHeaderMap() {
        return new JsonResult<>(ExcelHelper.getTableHeads(WeeklySummaryReportExportModel.class));
    }

    /**
    * 实体列表转excel列表
    *
    * @param weeklySummaryReportList
    * @return
    */
    private List<WeeklySummaryReportExportModel> entityList2ExcelList(List<WeeklySummaryReportListVO> weeklySummaryReportList) {
        if (V.isEmpty(weeklySummaryReportList)) {
            return Collections.emptyList();
        }
        return BeanUtils.convertList(weeklySummaryReportList, WeeklySummaryReportExportModel.class);
    }

    @Override
    protected ReadExcelListener<WeeklySummaryReportImportModel> getExcelDataListener() {
        return new WeeklySummaryReportImportListener();
    }
}